package com.tjbank.cssys.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkOutsourcingCompany;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOutsourcingCompanyService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import com.tjbank.cssys.web.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = "CkOutsourcingCompanyController", description = "委外公司管理")
@RestController
@RequestMapping("/outsourcingCompany")
public class CkOutsourcingCompanyController  {
    @Autowired
    private CkOutsourcingCompanyService ckOutsourcingCompanyService;

    @Autowired
    private CkLogService ckLogService;
    private static Logger logger = LoggerFactory.getLogger(CkOutsourcingCompanyController.class);

    @ApiOperation("获取委外公司查询管理")
    @PostMapping("/list")
    public CommonResult<PageInfo> getOutsourcingCompanyList(@RequestBody Map<String, Object> params, HttpServletRequest request){
        PageInfo pageInfo=ckOutsourcingCompanyService.getOutsourcingCompanyList(params);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取委外公司查询管理异常",e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("获取委外公司查询");

        if (pageInfo != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return  CommonResult.success(pageInfo);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }
    }

    @ApiOperation("查询委外公司详情，含分案列表和对账列表")
    @PostMapping("/details")
    public CommonResult getDetails(@RequestParam Integer companyId, HttpServletRequest request){
        String userCode = null;
        CommonResult result = null;
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("companyId=="+companyId);
        ckLog.setOperationType("获取委外公司详情");
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
            ckLog.setUserId(userCode);

            Map<String, Object> companyDetails = ckOutsourcingCompanyService.getCompanyDetails(companyId);
            result = CommonResult.success(companyDetails);

            ckLog.setResult("success:200");

        } catch (Exception e) {
            result = CommonResult.failed("错误");
            logger.error("获取委外公司查询管理异常",e.getMessage());
            ckLog.setResult("error:500"+e.getMessage());
        }finally {
            //ckLogService.addInsert(ckLog);
        }
        return  result;
    }


    @ApiOperation("更新委外公司")
    @PostMapping("/update/old")
    @Deprecated
    public CommonResult updateOutsourcingCompany(@RequestBody CkOutsourcingCompany ckOutsourcingCompany, HttpServletRequest request){
        ckOutsourcingCompanyService.updateOutsourcingCompany(ckOutsourcingCompany);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("更新委外公司异常",e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(ckOutsourcingCompany+"");
        ckLog.setOperationType("更新委外公司");
        ckLog.setResult("success:200");

        return  CommonResult.success("更新委外公司成功");
    }

    /**
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新委外公司")
    public CommonResult newUpdateCompany(@RequestBody Map<String,Object> params, HttpServletRequest request){
        String userCode = null;
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(JSONObject.toJSONString(params));
        ckLog.setOperationType("更新委外公司");

        CommonResult result = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
            ckOutsourcingCompanyService.updateCompanyInfoAndLevelsAndRate(params);
            ckLog.setResult(ResultCode.SUCCESS.getCode()+"");
            result = CommonResult.success(null,"更新委外公司成功");
        } catch (Exception e) {
            logger.error("更新委外公司异常",e.getMessage());
            ckLog.setResult(ResultCode.FAILED.getCode()+"");
            result = CommonResult.failed(null,"更新委外公司成功");
        }finally {
            ckLogService.addInsert(ckLog);
        }

        return  result;
    }

}
